package com.cgbl.cg.ui.resource

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.blankj.utilcode.util.LogUtils
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.FilterResourceTabAdapter
import com.cgbl.cg.adapter.SearchResourceAdapter
import com.cgbl.cg.databinding.AppFragmentResourceMenuBinding
import com.cgbl.cg.vm.resource.ResourceMenuFraViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.FilterBean
import com.robot.base.model.ResourceBean
import com.robot.base.util.LiveDataEventManager

class ResourceMenuFragment :
    GLBaseFragment<AppFragmentResourceMenuBinding, ResourceMenuFraViewModel>() {

    private lateinit var searchResourceAdapter: SearchResourceAdapter
    private lateinit var filterResourceTabAdapter: FilterResourceTabAdapter
    var typeTab = ""
    var filterBean = FilterBean()

    companion object {
        fun newInstance(typeTab: String, filterBean: FilterBean): ResourceMenuFragment {
            val fragment = ResourceMenuFragment()
            val bundle = Bundle()
            bundle.putString("typeTab", typeTab)
            bundle.putSerializable("filterBean", filterBean)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_resource_menu

    override fun initParam() {
        super.initParam()
        typeTab = arguments?.getString("typeTab") ?: ""
        val bean = arguments?.getSerializable("filterBean")
        if (bean != null) {
            filterBean = bean as FilterBean
        }
    }

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.type = typeTab
        viewModel.initData(filterBean)

    }

    private fun initAdapter() {
        searchResourceAdapter = SearchResourceAdapter()
        binding.recyclerView.apply {
            adapter = searchResourceAdapter
        }

        filterResourceTabAdapter = FilterResourceTabAdapter()
        binding.recyclerViewTab.apply {
            adapter = filterResourceTabAdapter
        }

        filterResourceTabAdapter.addChildClickViewIds(R.id.item_iv_close)
        filterResourceTabAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_iv_close -> {
                    filterResourceTabAdapter.removeAt(position)
                    val filterBean = FilterBean()
                    filterResourceTabAdapter.data.forEach {
                        when (it.type) {
                            AppConstants.BundleKey.RESOURCES_TYPE -> {
                                filterBean.resourcesType = it
                            }
                            AppConstants.BundleKey.WIRING_Type -> {
                                filterBean.wiringType = it
                            }
                            AppConstants.BundleKey.PRICE -> {
                                filterBean.isFree = it
                            }
                            AppConstants.BundleKey.MS -> {
                                filterBean.ms = it
                            }
                        }
                    }
                    LiveDataEventManager.postResourceFilterEvent(filterBean)
                }
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        LiveDataEventManager.observerResourceFilterEvent(this) {
            LogUtils.e(it)
            it ?: return@observerResourceFilterEvent
            viewModel.initData(it)
        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.load()
        }

        viewModel.uc.refreshStatusEvent.observe(this) {
            binding.smartRefreshLayout.finishLoadMore()
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.setEnableLoadMore(it != 1)
        }

        viewModel.uc.refreshDataEvent.observe(this) {
            if (viewModel.pageNo == 1) {
                searchResourceAdapter.setNewInstance(it as MutableList<ResourceBean>?)
            } else {
                searchResourceAdapter.addData(it)
            }
        }

        viewModel.uc.refreshTabEvent.observe(this) {
            LogUtils.e(it.size)
            filterResourceTabAdapter.setNewInstance(it as MutableList<FilterBean.FilterChildBean>?)
        }
    }

}